
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menuCadLivro.java
 *
 * Created on 16/08/2011, 14:55:41
 */
/**
 *
 * @author Anthony
 */
public class menuCadLivro extends javax.swing.JFrame {

    /** Creates new form menuCadLivro */
    public menuCadLivro() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        campoTitulo = new javax.swing.JTextField();
        campoEditora = new javax.swing.JTextField();
        campoAno = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(102, 102, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Nome");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, -1, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Titulo");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Editora");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Ano");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 150, -1, -1));
        jPanel1.add(campoNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, 80, -1));
        jPanel1.add(campoTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 80, -1));
        jPanel1.add(campoEditora, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 110, 80, -1));
        jPanel1.add(campoAno, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 150, 80, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Confirmar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 210, -1, -1));

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 210, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 240, 240));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
            try{ 
            Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String nome = campoNome.getText();
            String titulo = campoTitulo.getText();
            String editora = campoEditora.getText();
            String ano = campoAno.getText();
            String sql = "select nome,titulo from livro where nome = ? and titulo = ?";
            
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, nome);
            query.setString(2, titulo);
            ResultSet rs = query.executeQuery();
            
             if(rs.next()){
                JOptionPane.showMessageDialog(null, "Livro já cadastrado no banco!");
             }
             else {
                    
                if(campoNome.getText().equals(" ") || campoTitulo.getText().equals(" ") || campoEditora.getText().equals(" ") || campoAno.getText().equals(" ")){
                    JOptionPane.showMessageDialog(null, "campo(s) não preenchido!");
                }
                else {
                   
                    sql = "insert into livro(nome,titulo,editora,ano) values(?,?,?,?)";
                    query = conexao.prepareStatement(sql);
                    query.setString(1, nome);
                    query.setString(2, titulo);
                    query.setString(3, editora);
                    query.setString(4, ano);
                    query.execute();
                    JOptionPane.showMessageDialog(null, "Livro cadastrado com sucesso!");
                    this.dispose();
                }
             }
                
                
                
                
                
             
            }catch(Exception e){
            System.out.println("Deu erro...");
            e.printStackTrace();
        }
       
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new menuCadLivro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoAno;
    private javax.swing.JTextField campoEditora;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextField campoTitulo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
